*************************************************
* Purpose -- Maps with BCPs and MSCZS
************************************************
clear all
* RUN 00_path_master.do FIRST TO GET FILEPATHS

*-------------------------------------------------------------------------------
* Prepare data with coordinates
*-------------------------------------------------------------------------------
* Install maptile county coordinattes
maptile_install using "$raw/other/geo_county1990.zip", replace

* Coordinates file with MSCZ and all state borders
use "$raw/jnr-cbp/czone_multi.dta"

merge 1:1 cz using "$raw/other/geo_cz1990/cz1990_database.dta"
keep if multi == 1
keep id
rename id _ID
tempfile core
save `core', replace

use "$raw/other/geo_cz1990/cz1990_coords.dta", clear
merge m:1 _ID using `core', nogen keep(3)
gen geo = 2

append using "$raw/other/geo_state/state_coords_clean.dta"
replace geo = 1 if geo == .
drop if geo ==1 & inlist(_ID, 27, 8)

save "$clean/other/cz1990_samp_state_coords.dta", replace


* Coordinates file with MSCZ with MW variation and all state borders
use "$raw/jnr-cbp/cbp_stacked_czonestatepair_sample.dta", clear

bysort czonestatepair year: egen max_mw = max(mw)
bysort czonestatepair year: egen min_mw = min(mw)
gen mw_diff = max_mw-min_mw
bysort czonestatepair: egen max_diff = max(mw_diff)
keep if max_diff > 0
collapse (max) mw_diff, by(czone)
keep czone
rename czone cz

merge 1:1 cz using "$raw/other/geo_cz1990/cz1990_database.dta", keep(3)
keep id
rename id _ID
tempfile core
save `core', replace

use "$raw/other/geo_cz1990/cz1990_coords.dta", clear
merge m:1 _ID using `core', nogen keep(3)
gen geo = 2

append using "$raw/other/geo_state/state_coords_clean.dta"
replace geo = 1 if geo == .
drop if geo ==1 & inlist(_ID, 27, 8)

save "$clean/other/cz1990_samp_state_coords_mw.dta", replace

* Coordinates file with state borders
keep if geo == 1
save "$clean/other/state_coords_geo.dta", replace

* get county borders

use "$raw/other/geo_county1990/county1990_coords.dta", clear

preserve
	use "$raw/other/geo_county1990/county1990_database.dta", clear
	tostring county, gen(county_str)
	drop if strpos(county_str,"2") == 1 & strlen(county_str) == 4
	drop if strpos(county_str,"15") == 1 & strlen(county_str) == 5
	rename id _ID
	keep _ID
	tempfile counties
	save `counties'
restore

merge m:1 _ID using `counties', assert(1 3) keep(3) nogen
gen geo = 2
append using "$clean/other/state_coords_geo.dta"
save "$clean/other/state_county_coords_geo.dta", replace
	

*-------------------------------------------------------------------------------
* Map including state, CZ and county borders with MW variation
*-------------------------------------------------------------------------------
use "$raw/jnr-cbp/cbp_stacked_countypair_sample.dta", clear 
merge m:1 countypair_id county using "$raw/jnr-cbp/countypairtypes.dta", nogen
keep if type0 == 1
drop type*

bysort countypair_id year: egen max_mw = max(mw)
bysort countypair_id year: egen min_mw = min(mw)
gen mw_diff = max_mw-min_mw
bysort countypair_id: egen max_diff = max(mw_diff)
keep if max_diff > 0
collapse (max) max_diff , by(countypair county)

gen multishade = 1

collapse (max) multishade (first) countypair, by(county)

cd "$clean"

maptile multishade, geo(county1990) conus fc(Blues2) ///
cutvalues(1(1)10) ndf(gray*.1) ///
spopt( ///
	polygon(data("$clean/other/cz1990_samp_state_coords_mw.dta") ///
	by(geo) legenda(off) ///
	ocolor(black orange_red) osize(thin medthin)) ///
) ///
twopt( ///
	legend(pos(7) row(6) order(2 "Border counties" 1 "Out-of-samples counties" 0 "Multistate CZs")) ///
	text(35.6 -123.2 "_", size(vlarge) placement(west) color(orange_red)) ///
	title(" ") ///
)

gr export "$figures/Figure A2_B.png", replace

*-------------------------------------------------------------------------------
* Map including state, CZ and county borders
*-------------------------------------------------------------------------------
use "$raw/jnr-cbp/countypairtypes.dta", clear 

gen multishade = .
replace multishade = 1 if type0 == 1

collapse (max) multishade (first) countypair type0 type1 type2 type3 type4 type5, by(county)

maptile multishade if type0 == 1, geo(county1990) conus fc(Blues2) ///
cutvalues(1(1)10) ndf(gray*.1) ///
spopt( ///
	polygon(data("$clean/other/cz1990_samp_state_coords.dta") ///
	by(geo) legenda(off) ///
	ocolor(black orange_red) osize(thin medthin)) ///
) ///
twopt( ///
	legend(pos(7) row(6) order(2 "Border counties" 1 "Out-of-samples counties" 0 "Multistate CZs")) ///
	text(35.6 -123.2 "_", size(vlarge) placement(west) color(orange_red)) ///
	title(" ") ///
)

gr export "$figures/Figure A2_A.png", replace

